package com.app.annotationlib;

import com.google.auto.service.AutoService;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVariable;
import javax.tools.Diagnostic;


@AutoService(Processor.class)
public class ZButterKnifeProcessor extends AbstractProcessor {

    private Messager mMessage;
    private Filer filer;
    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        System.out.println("zhouwd -> ZButterKnifeProcessor -> init");
        mMessage = processingEnv.getMessager();
        filer = processingEnv.getFiler();
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

        for (Element element : roundEnv.getRootElements()) {
            if (element instanceof TypeElement) {
                TypeElement typeElement = (TypeElement) element;
                System.out.println("zhouwd -> getQualifiedName -> " + typeElement.getQualifiedName());
            }
        }

        for(Element element : roundEnv.getElementsAnnotatedWith(ZBindView.class)){
            if(element.getKind() == ElementKind.FIELD){
                mMessage.printMessage(Diagnostic.Kind.NOTE,"zhouwd -> printMessage : " + element.toString());
                System.out.println("zhouwd -> ZButterKnifeProcessor -> printMessage :" + element.toString());
            }

            Name outSimpleName = element.getSimpleName();
            System.out.println("zhouwd -> ZButterKnifeProcessor -> outSimpleName :" + outSimpleName.toString());

            ElementKind outKind = element.getKind();
            System.out.println("zhouwd -> ZButterKnifeProcessor -> outKind :" + outKind.name());


            TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();

            Name simpleName = enclosingElement.getSimpleName();
            Name qualifiedName = enclosingElement.getQualifiedName();
            System.out.println("zhouwd -> ZButterKnifeProcessor -> simpleName :" + simpleName.toString());
            System.out.println("zhouwd -> ZButterKnifeProcessor -> qualifiedName :" + qualifiedName.toString());

            TypeMirror elementType = element.asType();
            if (elementType.getKind() == TypeKind.TYPEVAR) {
                TypeVariable typeVariable = (TypeVariable) elementType;
                elementType = typeVariable.getUpperBound();
            }

            System.out.println("zhouwd -> ZButterKnifeProcessor -> elementType :" + elementType.getKind().name());

        }

        return true;
    }

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        //支持的注解
        Set<String> annotations = new LinkedHashSet<>();
        annotations.add(ZBindView.class.getCanonicalName());
        return annotations;
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }
}
